package com.open.capacity.finance.service.impl;

import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.finance.entity.SpringScheduledCron;
import com.open.capacity.finance.dao.SpringScheduledCronDao;
import com.open.capacity.finance.service.SpringScheduledCronService;


@Service
public class SpringScheduledCronServiceImpl implements SpringScheduledCronService {

    @Autowired
    private SpringScheduledCronDao springScheduledCronDao;

    /**
     * 添加
     *
     * @param springScheduledCron
     */
    public int save(SpringScheduledCron springScheduledCron) {
        springScheduledCron.setStatus(1);
        return springScheduledCronDao.save(springScheduledCron);
    }

    /**
     * 修改
     *
     * @param springScheduledCron
     */
    public int update(SpringScheduledCron springScheduledCron) {
        return springScheduledCronDao.update(springScheduledCron);
    }

    @Override
    public int updateExcuteTime(Integer cronId, Date excuteTime, Integer excuteStatus, String message, Long timeConsuming) {
        return springScheduledCronDao.updateExcuteTime(cronId, excuteTime, excuteStatus, message, timeConsuming);
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(Integer id) {
        return springScheduledCronDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public SpringScheduledCron findById(Integer id) {
        return springScheduledCronDao.findById(id);
    }

    @Override
    public SpringScheduledCron findByKey(String key) {
        return springScheduledCronDao.findByKey(key);
    }


    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<SpringScheduledCron> pageAll(Map<String, Object> params) {
        Page<SpringScheduledCron> startPage = null;
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null) {
            startPage = PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);
            startPage.setOrderBy("cron_id");
        }
        List<SpringScheduledCron> list = springScheduledCronDao.findAll(params);
        PageInfo<SpringScheduledCron> pageInfo = new PageInfo(list);

        return PageResult.<SpringScheduledCron>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<SpringScheduledCron> findAll(Map<String, Object> params) {
        return springScheduledCronDao.findAll(params);
    }

}
